﻿using MyChat.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntdUI;

namespace MyChat.ViewModel
{
    public class ModbusViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;

        private ModbusTCPModel _modbusModel;
        private ModbusTcpView _ModbusTcpView;
        public ModbusViewModel() 
        {
            _modbusModel = new ModbusTCPModel(); // 初始化 ModbusTCPModel
            //_ModbusTcpView = new ModbusTcpView();
        }
        private string _ipAddress;
        private int _port;
        private string _statusMessage;
        private string _readData;
        //private ModbusTcpView ModbusTcpView;

        // IP 地址
        public string IpAddress
        {
            get { return _ipAddress; }
            set
            {
                _ipAddress = value;
                OnPropertyChanged("IpAddress");
            }
        }

        // 端口
        public int Port
        {
            get { return _port; }
            set
            {
                _port = value;
                OnPropertyChanged("Port");
            }
        }

        // 状态消息
        public string StatusMessage
        {
            get { return _statusMessage; }
            set
            {
                _statusMessage = value;
                OnPropertyChanged("StatusMessage");
            }
        }
        // 连接到 Modbus 服务器
        public void Connect()
        {
            if (_modbusModel.Connect(IpAddress, Port))
            {
                AntdUI.Notification.info(_ModbusTcpView, "提示", "连接成功");
                //StatusMessage = "连接成功!";
                
            }
            else
            {
                //StatusMessage = "连接失败!";
            }
        }

        // 断开连接
        public void Disconnect()
        {
            _modbusModel.Disconnect();
            AntdUI.Notification.info(_ModbusTcpView, "提示","已断开连接");
        }

        // 通知属性改变
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
